import matplotlib.pyplot as plt
import pandas as pd
from matplotlib.ticker import FuncFormatter

# 定义一个格式化函数，用于将数值转换为带有千位分隔符的字符串
def format_x(x, pos):
    return f"${x:,.0f}"

# 读取CSV文件，第一行作为列名
df = pd.read_csv('sensitivity_table-副本.csv', header=0)

# 选择1列和2列作为x和y轴的自变量
x = df['Unit CLAP/day\n[$]']
y = df['Unit PLAC/day\n[$]']

# 选择2、21、25、26列作为因变量
z1 = df.iloc[:, 2]  # 第2列:Ratio of unit CLAP to unit PLAC
z2 = df.iloc[:, 3]  # 第3列:Aver. TAC of base schedule[$]
z3 = df.iloc[:, 22]  # 第22列:Aver. suggested postponement[day]
z4 = df.iloc[:, 23]  # 第23列:Aver. reduced TAC[$]
z5 = df.iloc[:, 24]  # 第24列:Aver. CLAP[$]
z6 = df.iloc[:, 26]  # 第26列:Aver. PLAC[$]

# 设置图形的大小
plt.figure(figsize=(15, 10))  # 调整图形大小以适应3个子图

# 创建一个1行3列的subplot网格
fig, axs = plt.subplots(3, 1, figsize=(15, 10))  # 3行1列

# 循环遍历每个子图，绘制数据
for i, ax in enumerate(axs):
    # 设置子图标题
    ax.set_title(f"Subplot {i+1}")

    # 绘制数据

    ax.plot(y[0:3], z1[0:3], marker='^', linestyle='-', color='red', label='Ratio of unit CLAP to unit PLAC')
    ax.plot(y[0:3], z2[0:3], marker='v', linestyle='-.', color='orange', label='Avg. TAC of base schedule[$]')
    ax.plot(y[0:3], z3[0:3], marker='s', linestyle=(0, (1, 10)), color='black', label='Avg. suggested postponement[day]')

    # 创建第二个轴，共享x轴，用于右侧y轴
    ax2 = ax.twinx()
    ax2.plot(y[0:3], z4[0:3], marker='d', linestyle=(0, (3, 10, 1, 10)), color='green', label='Avg. reduced TAC[$]')
    ax2.plot(y[0:3], z5[0:3], marker='o', linestyle='--', color='blue', label='Avg. CLAP[$]')
    ax2.plot(y[0:3], z6[0:3], marker='+', linestyle=':', color='purple', label='Avg. PLAC[$]')
    ax2.set_ylabel('Additional cost[$]')


    # 添加图例
    ax.legend(loc='upper left')
    ax2.legend(loc='upper right')

# 调整子图间距
plt.tight_layout()

# 显示图形
plt.show()
